﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:VchtypeDetails
    /// </summary>
    public partial class VchtypeDetails
    {
        public VchtypeDetails()
        {
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("F_ID", "VchtypeDetails");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int F_ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from VchtypeDetails");
            strSql.Append(" where F_ID=@F_ID");
            var parameters = new SqlParameter[] { new SqlParameter("@F_ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = F_ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DMZS.Model.VchtypeDetails model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into VchtypeDetails(");
            strSql.Append("vchtype,billhead,billdetail,billdetail1,tabhead,tadetail,fkeys)");
            strSql.Append(" values (");
            strSql.Append("@vchtype,@billhead,@billdetail,@billdetail1,@tabhead,@tadetail,@fkeys)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@vchtype", SqlDbType.Int, 4),
                    new SqlParameter("@billhead", SqlDbType.Text),
                    new SqlParameter("@billdetail", SqlDbType.Text),
                    new SqlParameter("@billdetail1", SqlDbType.Text),
                    new SqlParameter("@tabhead", SqlDbType.VarChar, 100),
                    new SqlParameter("@tadetail", SqlDbType.VarChar, 100),
                    new SqlParameter("@fkeys", SqlDbType.VarChar, 100) };
            parameters[0].Value = model.vchtype;
            parameters[1].Value = model.billhead;
            parameters[2].Value = model.billdetail;
            parameters[3].Value = model.billdetail1;
            parameters[4].Value = model.tabhead;
            parameters[5].Value = model.tadetail;
            parameters[6].Value = model.fkeys;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.VchtypeDetails model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update VchtypeDetails set ");
            strSql.Append("vchtype=@vchtype,");
            strSql.Append("billhead=@billhead,");
            strSql.Append("billdetail=@billdetail,");
            strSql.Append("billdetail1=@billdetail1,");
            strSql.Append("tabhead=@tabhead,");
            strSql.Append("tadetail=@tadetail,");
            strSql.Append("fkeys=@fkeys");
            strSql.Append(" where F_ID=@F_ID");
            var parameters = new SqlParameter[] { new SqlParameter("@vchtype", SqlDbType.Int, 4),
                    new SqlParameter("@billhead", SqlDbType.Text),
                    new SqlParameter("@billdetail", SqlDbType.Text),
                    new SqlParameter("@billdetail1", SqlDbType.Text),
                    new SqlParameter("@tabhead", SqlDbType.VarChar, 100),
                    new SqlParameter("@tadetail", SqlDbType.VarChar, 100),
                    new SqlParameter("@fkeys", SqlDbType.VarChar, 100),
                    new SqlParameter("@F_ID", SqlDbType.Int, 4) };
            parameters[0].Value = model.vchtype;
            parameters[1].Value = model.billhead;
            parameters[2].Value = model.billdetail;
            parameters[3].Value = model.billdetail1;
            parameters[4].Value = model.tabhead;
            parameters[5].Value = model.tadetail;
            parameters[6].Value = model.fkeys;
            parameters[7].Value = model.F_ID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int F_ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from VchtypeDetails ");
            strSql.Append(" where F_ID=@F_ID");
            var parameters = new SqlParameter[] { new SqlParameter("@F_ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = F_ID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string F_IDlist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from VchtypeDetails ");
            strSql.Append(" where F_ID in (" + F_IDlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.VchtypeDetails GetModel(int F_ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 F_ID,vchtype,billhead,billdetail,billdetail1,tabhead,tadetail,fkeys from VchtypeDetails ");
            strSql.Append(" where F_ID=@F_ID");
            var parameters = new SqlParameter[] { new SqlParameter("@F_ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = F_ID;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.VchtypeDetails DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.VchtypeDetails();
            if (row != null)
            {
                if (row["F_ID"] != null && row["F_ID"].ToString() != string.Empty)
                {
                    model.F_ID = int.Parse(row["F_ID"].ToString());
                }
                if (row["vchtype"] != null && row["vchtype"].ToString() != string.Empty)
                {
                    model.vchtype = int.Parse(row["vchtype"].ToString());
                }
                if (row["billhead"] != null)
                {
                    model.billhead = row["billhead"].ToString();
                }
                if (row["billdetail"] != null)
                {
                    model.billdetail = row["billdetail"].ToString();
                }
                if (row["billdetail1"] != null)
                {
                    model.billdetail1 = row["billdetail1"].ToString();
                }
                if (row["tabhead"] != null)
                {
                    model.tabhead = row["tabhead"].ToString();
                }
                if (row["tadetail"] != null)
                {
                    model.tadetail = row["tadetail"].ToString();
                }
                if (row["fkeys"] != null)
                {
                    model.fkeys = row["fkeys"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select F_ID,vchtype,billhead,billdetail,billdetail1,tabhead,tadetail,fkeys ");
            strSql.Append(" FROM VchtypeDetails ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" F_ID,vchtype,billhead,billdetail,billdetail1,tabhead,tadetail,fkeys ");
            strSql.Append(" FROM VchtypeDetails ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM VchtypeDetails ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.F_ID desc");
            }
            strSql.Append(")AS Row, T.*  from VchtypeDetails T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}

